#pragma strict
import System.Text;
///////////////////////////////////////////////////////////////////////////////////////////////////////
///蒙皮网格 骨骼替换
///思路:找到原始骨骼(Biped)(Transform)路径(到Bip01节点为止),然后换成另一个模型的(Biped)的骨骼..
///////////////////////////////////////////////////////////////////////////////////////////////////////
var bonesInfo:String[];  	//骨骼名称数组
var bones:Transform[]; 		//新找到的骨骼数组
/** 主骨骼节点:从此节点中开始获取全部的骨骼节点(Transform) */
var rootTransform:Transform;
/** rootBone的名字 */
var rootBoneName:String = "Bip01";

var isReplaceBonesWhenStart:boolean = true;

private var tr:Transform;

function Start () {
	tr = transform;
	rootTransform = tr.root; 
	
	if(isReplaceBonesWhenStart)
		ReplaceBones(GetComponent.<SkinnedMeshRenderer>());
}

/** 将skinned里面的bones重新指定为 rootTransform中的骨骼. */
function ReplaceBones(skinned:SkinnedMeshRenderer){
	if(skinned){
		var rootBone:Transform = skinned.rootBone;
		rootBone = rootTransform.Find(TransformHelper.GetAbsolutionPath(rootBone,rootBoneName));
		
		var bones:Transform[] = new Transform[skinned.bones.length];
		var i:int = 0;
		for(var bone:Transform in skinned.bones){
			bones[i++] = rootTransform.Find(TransformHelper.GetAbsolutionPath(bone,rootBoneName));
		}
		
		skinned.bones = bones;
		skinned.rootBone = rootBone;
	}
}
/** 重新生成 skinned 
private function RegenerateSkinnedMeshRenderer(){
	//新节点
	var go:GameObject = GameObject(tr.gameObject.name);
	go.transform.parent = tr.parent;
	var renderer:SkinnedMeshRenderer = go.AddComponent(SkinnedMeshRenderer);
	renderer.bones = bones;
	renderer.sharedMesh = skinned.sharedMesh;
	renderer.materials = skinned.materials;
	//删除旧 节点
	Destroy(gameObject);
}
*/